package com.bainuo.doctor.common.base;

import android.content.Context;
import android.support.v7.widget.RecyclerView;

import com.bainuo.doctor.common.base.EmptyBaseAdapter;
import com.bainuo.doctor.common.base.LoadMoreWrapper;
import com.bainuo.doctor.common.widget.CustomRefreshLayout;
import com.bainuo.doctor.common.widget.ptr.PtrDefaultHandler;
import com.bainuo.doctor.common.widget.ptr.PtrFrameLayout;

import java.util.List;

/**
 * Created by faraway on 9/8/17.
 */

public class CommonRefresh {

    RecyclerView mRv;
    CustomRefreshLayout mRefreshLayout;

    public int mCurrentPage = 1;
    public boolean isDown = true;
    public LoadMoreWrapper mLoadMoreWrapper;
    private EmptyBaseAdapter mAdapter;

    public CommonRefresh(Context context, RecyclerView rv, CustomRefreshLayout refreshLayout, EmptyBaseAdapter adapter){

        mRv = rv;
        mRefreshLayout = refreshLayout;
        mAdapter = adapter;

        mRefreshLayout.disableWhenHorizontalMove(true);
        mRefreshLayout.setPtrHandler(new PtrDefaultHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                mCurrentPage = 1;
                isDown = true;
                if (callback != null)
                    callback.requestData();
            }
        });

        mLoadMoreWrapper = new LoadMoreWrapper(context, adapter);
        mLoadMoreWrapper.hideLoadMoreView();
        mRv.setAdapter(mLoadMoreWrapper);
        mLoadMoreWrapper.setOnLoadListener(new LoadMoreWrapper.OnLoadListener() {
            @Override
            public void onRetry() {

            }

            @Override
            public void onLoadMore() {
                isDown = false;
                if (callback != null)
                    callback.requestData();
            }
        });
    }

    public interface GetDataCallback{
       void requestData();
    }

    private GetDataCallback callback;

    public void setCallback(GetDataCallback callback) {
        this.callback = callback;
    }

    public void onSuccess(List list,List newList, boolean next, int number) {

        mCurrentPage++;
        if (isDown) {
            list.clear();
        }

        if (newList != null)
            list.addAll(newList);

        mRefreshLayout.refreshComplete();

        if (next) {
            mLoadMoreWrapper.showLoadMore();
        } else if (list.size() < number) {
            mLoadMoreWrapper.hideLoadMoreView();
        } else {
            mLoadMoreWrapper.showLoadComplete();
        }

        if(list.size() >0){
            mAdapter.setStatus(EmptyBaseAdapter.STATUS_HAS_DATA);
        }else{
            mAdapter.setStatus(EmptyBaseAdapter.STATUS_EMPTY_DATA);
        }

        mLoadMoreWrapper.notifyDataSetChanged();
    }

    public void onSuccess(List list, boolean next, int number) {

        mCurrentPage++;

        mRefreshLayout.refreshComplete();

        if (next) {
            mLoadMoreWrapper.showLoadMore();
        } else if (list.size() < number) {
            mLoadMoreWrapper.hideLoadMoreView();
        } else {
            mLoadMoreWrapper.showLoadComplete();
        }

        if(list.size() >0){
            mAdapter.setStatus(EmptyBaseAdapter.STATUS_HAS_DATA);
        }else{
            mAdapter.setStatus(EmptyBaseAdapter.STATUS_EMPTY_DATA);
        }

        mLoadMoreWrapper.notifyDataSetChanged();
    }


    public void onFailed(){
        if(mRefreshLayout != null){
            mRefreshLayout.refreshComplete();
        }
        if(mLoadMoreWrapper != null){
            mLoadMoreWrapper.showLoadError();
        }
    }

}
